const express = require('express')
const userListRouter = express.Router()
const { userListModel } = require('../../Schema/userAdminProject/userListSchema')

// 差
userListRouter.get('', async(req, res) => {
    let { size = 2, page = 0, query } = req.query;
    let queryObj = {};
    if (query) {
        queryObj = {
            $or: [
                { name: { $regex: query } },
                { age: { $regex: query } },
            ]
        }
    }
    //分页
    let userResultArr = await userListModel.find(queryObj).sort({ _id: 1 }).limit(+size).skip(size * page)
    res.send({ code: 0, msg: userResultArr })
})

//增

userListRouter.post('', async(req, res) => {
    // console.log(req.body);
    let userResult = await userListModel.create(req.body)
    res.send({ code: 0, msg: '添加成功' })
})

//改
userListRouter.put('', async(req, res) => {
    let { _id } = req.body;
    let updateResult = await userListModel.updateOne({ _id }, req.body)
    console.log(updateResult);
    res.send({ code: 0, msg: updateResult })
})

//删
userListRouter.delete('', async(req, res) => {
    let ids = req.body._id.split(',');
    let r = await userListModel.remove({ _id: { $in: ids } })

})

module.exports = {
    userListRouter
}